package com.sfeir.poc.portal.gui.client.util;

import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.user.client.ui.HTMLPanel;
import com.sfeir.poc.portal.gui.client.EventAwareClass;
import com.sfeir.poc.portal.gui.client.events.ApplicationEvent;
import com.sfeir.poc.portal.gui.client.events.ApplicationEventType;
import com.sfeir.poc.portal.gui.client.objects.Position;
import com.sfeir.poc.portal.gui.client.objects.WidgetDescription;
import com.sfeir.poc.portal.gui.client.widgets.CalcWidget;
import com.sfeir.poc.portal.gui.client.widgets.GoogleMapWidget;
import com.sfeir.poc.portal.gui.client.widgets.NewsWidget;
import com.sfeir.poc.portal.gui.client.widgets.RssWidget;
import com.sfeir.poc.portal.gui.client.widgets.SlideshareWidget;
import com.sfeir.poc.portal.gui.client.widgets.SlideshowWidget;
import com.sfeir.poc.portal.gui.client.widgets.VpodTVWidget;
import com.sfeir.poc.portal.gui.client.widgets.YoutubeWidget;

/**
 * This class load the appropriate widget according the the WidgetDescription Type
 * @author bostoen.v
 *
 */
public class WidgetBuilder extends EventAwareClass {

	public void createWidgetsView(HashMap<Position, WidgetDescription> widgets) {
		HashMap<Position, WidgetDescription> widgetsList = new HashMap<Position, WidgetDescription>();
		Position position = null;
		WidgetDescription currentWidget = null;
		for (Iterator<Position> iterator = widgets.keySet().iterator(); iterator.hasNext();) {
			position = (Position) iterator.next();
			currentWidget = widgets.get(position);
			
			if (currentWidget.getType().compareTo("rss") == 0) {
				currentWidget.setWidgetRepresentation(new RssWidget(currentWidget));
			} else if(currentWidget.getType().compareTo("calc") == 0){
				currentWidget.setWidgetRepresentation(new CalcWidget());
			}else if(currentWidget.getType().compareTo("youtube") == 0){
				currentWidget.setWidgetRepresentation(new YoutubeWidget(currentWidget.getUrl()));
			}else if(currentWidget.getType().compareTo("slideshare") == 0){
				currentWidget.setWidgetRepresentation(new SlideshareWidget(currentWidget.getUrl()));
			}else if(currentWidget.getType().compareTo("vpodtv") == 0){
				currentWidget.setWidgetRepresentation(new VpodTVWidget(currentWidget.getUrl()));
			}else if(currentWidget.getType().compareTo("googlemap") == 0){
				currentWidget.setWidgetRepresentation(new GoogleMapWidget(currentWidget.getUrl()));
			}
			else {
				currentWidget.setContent(getHtmlForWidget(currentWidget));
				HTMLPanel panel = new HTMLPanel(currentWidget.getContent());
				currentWidget.setWidgetRepresentation(panel);
				widgetsList.put(position, currentWidget);
			}
		}
		fireActionEvent(ApplicationEventType.RENDER_WIDGETS, widgets);
	}

	public void updateWidgetView(WidgetDescription widgetDescription){
		if (widgetDescription.getType().compareTo("spreadsheet") == 0) {
			widgetDescription.setWidgetRepresentation(new NewsWidget(widgetDescription));
		} else if(widgetDescription.getType().compareTo("rss") == 0) {
			widgetDescription.setWidgetRepresentation(new RssWidget(widgetDescription));
		} else if(widgetDescription.getType().compareTo("slideshow") == 0) {
			widgetDescription.setWidgetRepresentation(new SlideshowWidget(widgetDescription.getNews()));
		} else if (widgetDescription.getType().compareTo("calc") == 0) {
				widgetDescription.setWidgetRepresentation(new CalcWidget());
		}else if(widgetDescription.getType().compareTo("youtube") == 0){
			widgetDescription.setWidgetRepresentation(new YoutubeWidget(widgetDescription.getUrl()));
		} else {
			widgetDescription.setContent(getHtmlForWidget(widgetDescription));
			HTMLPanel panel = new HTMLPanel(widgetDescription.getContent());
			widgetDescription.setWidgetRepresentation(panel);
		}
		fireActionEvent(ApplicationEventType.RENDER_WIDGET, widgetDescription);
	}
	
	private String getHtmlForWidget(WidgetDescription currentWidget) {
		String html = "";
		if(currentWidget.getType().compareTo("doc") == 0) {
			if (currentWidget.getArgs() != null && currentWidget.getArgs().get(0) != null) {
				html = "<iframe width=\"100%\" height=\"" + currentWidget.getArgs().get(0) + "\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"" + currentWidget.getUrl() + "\"></iframe><br />";
			} else {
				html = "<iframe width=\"100%\" height=\"600\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"" + currentWidget.getUrl() + "\"></iframe><br />";
			}
		} else if(currentWidget.getType().compareTo("lively") == 0) {
			html = "<iframe src='http://embed.lively.com/iframe?rid=3516548883177047187' width='400' height='400' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'></iframe>"; 
		} else if(currentWidget.getType().compareTo("qwerty") == 0) {
			html = "<iframe width=\"100%\" height=\"600\" src='http://nouvellecuisine.googlecode.com/svn/trunk/NouvelleCuisine/www/test.html'></iframe>";
		} else {
			html = "contenu";
		}
		return html;
	}

	@SuppressWarnings("unchecked")
	public void onActionFired(ApplicationEvent event) {
		super.onActionFired(event);
		if (event.getType().equals(ApplicationEventType.CREATE_WIDGET_VIEWS)) {
			createWidgetsView((HashMap<Position, WidgetDescription>) event.getContext());
		} else if (event.getType().equals(ApplicationEventType.CREATE_WIDGET_VIEW)) {
			updateWidgetView((WidgetDescription) event.getContext());
		}
	}
}
